package com.yh.csx.etl.util;

import java.util.Date;

import org.springframework.util.StringUtils;

import com.yh.csx.etl.support.SplitTypeEnum;

/**
 * 
 * 生成表后缀
 * 
 * @author Robin.Wang
 * @date 2020-03-22
 *
 */
public class SplitTypeUtils {
    public static final String TABLE_NAME_SPLIT = "_";

    /**
     * 生成后缀，如("_2020_03","_cold")
     * 
     * @param SplitTypeEnum 类型
     * @param format        格式
     * @param date          时间
     * @return
     * @throws Exception
     */
    public static String generateSuffix(SplitTypeEnum type, String format, Date date) throws Exception {
        switch (type) {
        case SOLID:
            return appendPrefix(format);
        case DAYLY:
        case MONTHLY:
        case YEARLY:
            return appendPrefix(DateUtils.formatValue(format, date));
        case WEEKYLY:
            return appendPrefix(DateUtils.formatValue(format, date)) + "W";
        case QUARTERLY:
            return appendPrefix(DateUtils.formatValue(format, date)) + "Q";
        default:
            return "";
        }
    }

    /**
     * 增加前缀
     */
    public static String appendPrefix(String source) {

        return appendPrefix(TABLE_NAME_SPLIT, source);
    }

    /**
     * 增加前缀
     */
    public static String appendPrefix(String suffix, String source) {
        if (StringUtils.hasText(source)) {
            if (source.startsWith(TABLE_NAME_SPLIT))
                return source;
            return TABLE_NAME_SPLIT + source;
        }
        return "";
    }

}
